package com.change.study.data.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.change.study.data.core.Finder;
import com.change.study.data.core.Pagination;
import com.change.study.data.core.Updater;
import com.change.study.data.dao.PostsDao;
import com.change.study.data.dao.UserLimitsDao;
import com.change.study.data.dao.UserPointsDao;
import com.change.study.data.dto.PostsPageRpc;
import com.change.study.data.entity.Posts;
import com.change.study.data.entity.UserLimits;
import com.change.study.data.entity.UserPoints;
import com.change.study.data.service.DataUtils;
import com.change.study.data.service.PostsService;

@Service
@Transactional
public class PostsServiceImpl implements PostsService {
	@Transactional(readOnly = true)
	public Pagination getPage(int pageNo, int pageSize) {
		Pagination page = dao.getPage(pageNo, pageSize);
		return page;
	}

	@Transactional(readOnly = true)
	public Posts findById(Long id) {
		Posts entity = dao.findById(id);
		return entity;
	}

	@Autowired
	private UserPointsDao userPointsDao;
	@Autowired
	private UserLimitsDao userLimitsDao;

	@Transactional
	public Posts save(Posts bean) {

		if (bean.getUser() == null) {
			bean=new Posts();
			bean.setId(-1l);
			return bean;
		}
		if(bean.getParentid()!=null){
			Posts parent=dao.findById(bean.getParentid());
			Integer size=parent.getCommentsize();
			if(size==null){
				size=0;
			}
			size++;
			parent.setCommentsize(size);
		}

		dao.save(bean);
		UserPoints points = userPointsDao.findByUserId(bean.getUser().getId());
		Integer s = points.getAposts() + DataUtils.getScore("apost");
		points.setAposts(s);

		UserLimits limits = userLimitsDao.findByUserId(bean.getUser().getId(),
				"addpost");
		return bean;
	}

	@Transactional
	public Posts update(Posts bean) {
		Updater<Posts> updater = new Updater<Posts>(bean);
		bean = dao.updateByUpdater(updater);
		return bean;
	}

	@Transactional
	public Posts deleteById(Long id) {
		Posts bean = dao.deleteById(id);
		return bean;
	}

	@Transactional
	public Posts[] deleteByIds(Long[] ids) {
		Posts[] beans = new Posts[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

	private PostsDao dao;

	@Autowired
	public void setDao(PostsDao dao) {
		this.dao = dao;
	}

	@Transactional
	@Override
	public Posts comment(Posts bean) {
		if (bean.getUser() == null) {
			return null;
		}
		bean.setType(Posts.C);
		dao.save(bean);
		UserPoints points = userPointsDao.findByUserId(bean.getUser().getId());
		Integer s = points.getCposts() + DataUtils.getScore("commentpost");
		points.setCposts(s);
		UserLimits limits = userLimitsDao.findByUserId(bean.getUser().getId(),
				"commentpost");
		return bean;
	}

	@Transactional(readOnly = true)
	@Override
	public PostsPageRpc pageCommentById(long id, int pageNo, int pageSize) {
		PostsPageRpc result = null;
		Finder finder = Finder.create();
		finder.append("from Posts p where p.type=3");

		finder.append(" and p.parentid=:id");
		finder.setParam("id", id);
		finder.append(" order by p.id desc");
		Pagination p = dao.find(finder, pageNo, pageSize);
		result = new PostsPageRpc(p);
		return result;
	}

	@Transactional(readOnly = true)
	@Override
	public PostsPageRpc page(int pageNo, int pageSize) {
		PostsPageRpc result = null;
		Finder finder = Finder.create();
		finder.append("from Posts p where p.type=1");

		finder.append(" order by p.id desc");
		Pagination p = dao.find(finder, pageNo, pageSize);
		result = new PostsPageRpc(p);
		
		
		return result;
	}
}